/*
 * CS_REQ_FAIRY_INFO.cpp
 *
 *  Created on: 2013. 9. 7.
 *      Author: jjaehuny
 */

//#include "../../engine.h"

#include "world.h"

#include "../../User.h"

#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Battle.h"

namespace ProjectZ { namespace NCommand { namespace NWorld {

void CS_REQ_FAIRY_INFO::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	STRESSLOG("[CHANNEL] CS_REQ_FAIRY_INFO::ONEXECUTE");
	PREPARE_PACKET;

	int userSeq = 0;

	*pPacket >> userSeq;

	uint64_t fairy_item_seq = 0;

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CS_REQ_FAIRY_INFO NULL == pSpace");
		return;
	}

	User * pTargetUser = pUser;

	if (userSeq != static_cast<int>(pUser->GetUserSeq()) )
	{
		if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::PVP_USER
				|| pSpace->GetData()._spaceType == NLogic::Space::SpaceType::NEW_PVP_ROOM)
		{
			pTargetUser = pUser->GetBattle()->GetData()._pvpOppUser;

			if( pTargetUser == NULL )
			{
				DEBUGLOG("CS_REQ_FAIRY_INFO NOT FOUND USER _pvpOppUser");
				return;
			}
		}
		else
		{
			pTargetUser = pSpace->FindUser(userSeq);
			if( pTargetUser == NULL )
			{
				DEBUGLOG("CS_REQ_FAIRY_INFO NOT FOUND USER");
				return;
			}
		}
	}

	uint64_t fairyItemSeq = pTargetUser->GetCharacterInfo().fairy;
	NLogic::Bag * pBag = pTargetUser->GetBag(BAG_TYPE_FAIRY);
	if (NULL == pBag)
	{
		DEBUGLOG("CS_REQ_FAIRY_INFO NULL == pBag");
		return;
	}



	NLogic::Item * pItem = pBag->GetItem(fairyItemSeq);
	if (NULL == pItem)
	{
		DEBUGLOG("CS_REQ_FAIRY_INFO NULL == pItem ITEM SEQ : %ld", fairyItemSeq);
		return;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	pAck->Write4(userSeq);
	pAck->Write2(pItem->GetData().bag_slot_number);
	pAck->MsgCat("%8d", fairy_item_seq);
	pAck->Write1(0);
	pAck->Write1(0);
	pAck->Write1(1);
	pAck->Write1(-1);
	pAck->Write1(0);
	pAck->Write1(0);
	pAck->Write1(pItem->GetData().quality);
	pAck->Write1(pItem->GetData().level);
	for( int i=0 ; i<7 ; i++ )
	{
		pAck->Write1(pItem->GetData().eff_type[i]);
		pAck->Write1(pItem->GetData().eff_pos[i]);
		pAck->Write2(pItem->GetData().eff_value[i]);
	}
	pAck->Write1(0);	//u8	open_upgrade_stone_slot; 	// 오픈된 강화석 슬롯수
	pAck->Write1(0);	//u8	ablity_enhance_rate; 		// 기본능력치 상향
	pAck->Write1(0);	//u8	max_enhance_step;			// 제련 최대값
	pAck->Write1(0);	//u8	buy_use;					// 구매 후 바로 사용 여부
	pAck->Write1(0);	//u8	evolve_step;				// 진화단계
	pAck->Write1(0);	//u8	evolve_max; 				// 진화 최대단계
	pAck->Write2(pItem->GetData().evolve_point);	//u16	evolve_point;				// 진화포인트
	pAck->Write2(pItem->GetData().evolve_percent);	//u16	evolve_eff;
	pAck->Write2(0);	//u16	evolve_eff_value;			// 진화이펙트 값
	pAck->Write1(pItem->GetData().class_type);	//u8	class_type;					// 장착클래스 타입
	pAck->Write1(pItem->GetData().sub_type);	//u8	sub_type;					// 서브타입
	pAck->Write2(pItem->GetData().tid);	//u16	tid;						// 테이블 아이디
	pAck->MakePacket(CMD_SC_REQ_FAIRY_INFO);
	pUser->Send(pAck);
}

} /*NWorld*/ } /*NCommand*/ } /*ProjectZ*/
